#ifndef PTHREAD_POOL_CPLUS_H
#define PTHREAD_POOL_CPLUS_H

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <map>
#include <string.h>


//定义默认的线程池最大值
#define MAX_THR_NUM 300
//线程池默认大小
#define DEFAULT_THR_NUM 100
//线程池默认增加大小
#define DEFAULT_THR_ADD 20
//线程池默认减少大小
#define DEFAULT_THR_DEL 20


/*ÈÎÎñœá¹¹Ìå*/
typedef struct tagPthreadPoolWork
{
    void* (*routine)(void*);
    void*  arg;
    struct tagPthreadPoolWork *next;

}TPthreadPoolWork;

typedef struct tagPthreadPool
{
    int now_thr_num;              			//线程池当前大小
    std::map<pthread_t,int> pthread_id_map; //k: pthread_id , v: 对应线程状态值 1 可销毁销毁, 0 正常
    int shutdown;                 			//线程池销毁标识 1:销毁,0:保持
	std::vector<TPthreadPoolWork> VecWork;	//任务容器
    pthread_mutex_t   queue_lock;			//线程锁
    pthread_cond_t    queue_ready;			//线程信号量

}TPthreadPool;

class CPthreadPool
{
    public:

        CPthreadPool();
        ~CPthreadPool();

        //创建线程池
        int pool_create();

        //销毁线程池
        void pool_destroy();

        //线程池增加工作者线程
        static void pool_add_pthread();

        //线程池减少工作者线程
        static void pool_del_pthread();

        //线程持释放空闲的工作者线程
        //static void pool_refree_pthread(); pthread_detach(pthread_self()) 已由此函数实现

        //向线程池添加任务
        int pool_add_work(void* (*routine)(void*),void *arg);

		//工作者线程
		static void* thread_routine(void* arg);

    private:

        static TPthreadPool m_tPthreadpool;
		
};

#endif
